home *** CD-ROM | disk | FTP | other *** search
/ Univers Mac Interactif 42 / Univers Mac Interactif - Issue 42.iso / Internet / MacHTTP 2.0 / MacHTTP Software & Docs / Tutorials / Extending MacHTTP Scripts / Script3.txt < prev    next >
Text File  |  1994-12-11  |  5KB  |  122 lines

  1. -- set these properties outside of the event.  That way they will only be set 
  2. -- once each time the app is run, not once for each event.
  3. property crlf : (ASCII character 13) & (ASCII character 10)
  4. -- This is a standard header for HTML files.
  5. property http_10_header : "HTTP/1.0 200 OK" & crlf & "Server: MacHTTP" & crlf & ¬
  6.     "MIME-Version: 1.0" & crlf & "Content-type: text/html" & crlf & crlf
  7. -- Idletime is how long you want it to remain open to 
  8. -- wait for another event. Idletime is in seconds.
  9. -- Datestamp will contain the current date.  Initialize it here.
  10. property idletime : 300 -- set to 5 minutes
  11. property datestamp : 0
  12.  
  13. -- this bit of code outside the sdoc event is executed at launch-time
  14. -- it is neccesary because an idle event can happen between
  15. -- launch and the receipt of an sdoc event (and in fact often does)
  16. set datestamp to current date
  17.  
  18. -- This is the loop for AppleEvents received by the application.
  19. -- When you check the syntax, AppleScript will ask you to locate 
  20. -- MacHTTP and will set the correct name at that time.
  21. on «event WWWΩsdoc» path_args ¬
  22.     given «class kfor»:http_search_args, «class post»:post_args, «class meth»:method, ¬
  23.     «class addr»:client_address, «class user»:username, «class pass»:password, «class frmu»:from_user, ¬
  24.     «class svnm»:server_name, «class svpt»:server_port, «class scnm»:script_name, «class ctyp»:content_type
  25.     -- Variables available for use:
  26.     -- http_search_args - stuff in the URL after a ?
  27.     -- post_args - stuff in the URL after a $
  28.     -- method - GET, POST, etc. Used to tell if post_args are valid
  29.     -- client_address - IP address or domain name of remote client's host
  30.     -- from_user - non-standard. e-mail address of remote user
  31.     -- username - authenticated user name
  32.     -- password - authenticated password
  33.     -- server_name - name or IP address of this server
  34.     -- server_port - TCP/IP port number being used by this server
  35.     -- script_name - URL name of this script
  36.     -- content_type - MIME content type of post_args
  37.     
  38.     -- save the current date and time to check later for quitting
  39.     set datestamp to current date
  40.     
  41.     -- Using the "try" clause causes the "on error" routine to be run
  42.     -- if an error occurs instead of crashing.
  43.     -- If the error occurs outside the "try"/"end try" space then
  44.     -- the "on error" routine is NOT run.
  45.     try
  46.         
  47.         -- the following line generates an error on purpose so you 
  48.         -- can see what the results look like.  Remove it before using
  49.         -- this code to build your own CGI or things won't work so well.
  50.         error "Unable to process field \"username\" of arg \"post_args\"" ¬
  51.             & return & "<BR>Field not recognized by script." ¬
  52.             number 100
  53.  
  54.         -- Return each parameter so you can see what the unprocessed
  55.         -- information looks like.
  56.     set return_page to http_10_header ¬
  57.         & "<HTML><HEAD><TITLE>Unprocessed Results</TITLE></HEAD>" ¬
  58.         & "<BODY><H1>Unprocessed Results</H1>" & return ¬
  59.         & "<H4>http_search_args</H4>" & return & http_search_args
  60.     set return_page to return_page & return ¬
  61.         & "<H4>post_args</H4>" & return & post_args & return ¬
  62.         & "<H4>method</H4>" & return & method & return ¬
  63.         & "<H4>client_address</H4>" & return & client_address & return
  64.     set return_page to return_page & return ¬
  65.         & "<H4>from_user</H4>" & return & from_user & return ¬
  66.         & "<H4>server_name</H4>" & return & server_name & return ¬
  67.         & "<H4>server_port</H4>" & return & server_port & return
  68.     set return_page to return_page & return ¬
  69.         & "<H4>script_name</H4>" & return & script_name & return ¬
  70.         & "<H4>content_type</H4>" & return & content_type & return ¬
  71.         & "<H4>username</H4>" & return & username & return ¬
  72.         & "<H4>password</H4>" & return
  73.     set return_page to return_page ¬
  74.         & "<HR><I>Results generated at: " & (current date) ¬
  75.         & "</I>" & "</BODY></HTML>"
  76.  
  77.         -- return the page created.  A return statement ends the 
  78.         -- processing of the AppleEvent
  79.    return return_page
  80.         
  81.         -- here is the routine to run if an error occurs
  82.         -- errMsg contains the message sent by the System
  83.         -- errNum contains the number of the error (negative for System, AE, or AS errors)
  84.     on error errMsg number errNum
  85.         -- create a page of HTML text to return
  86.         set return_page to http_10_header ¬
  87.             & ¬
  88.             "<HTML><HEAD><TITLE>Error Page</TITLE></HEAD>" & "<BODY><H1>Error Encountered!</H1>" & return ¬
  89.             & "An error was encountered while trying to run this script." & return
  90.         set return_page to return_page ¬
  91.             & "<H3>Error Message</H3>" & return & errMsg & return ¬
  92.             & "<H3>Error Number</H3>" & return & errNum & return ¬
  93.             & "<H3>Date</H3>" & return & (current date) & return
  94.         set return_page to return_page ¬
  95.             & ¬
  96.             "<HR>Please notify Jon Wiederspan at " & ¬
  97.             "<A HREF=\"mailto:jonwd@tjp.washington.edu\">jonwd@tjp.washington.edu</A>" & " of this error." & "</BODY></HTML>"
  98.         -- return the page created.  A return statement ends the 
  99.         -- processing of the AppleEvent
  100.         return return_page
  101.     end try
  102. end «event WWWΩsdoc»
  103.  
  104. -- The idle function is run everytime the system sends an idle message.
  105. -- If the current date is more than idletime seconds more than the last date 
  106. -- then it is time to quit.
  107. -- The return value tells the system how long to wait before
  108. on idle
  109.     if (current date) > (datestamp + idletime) then
  110.         quit
  111.     end if
  112.     return 5
  113. end idle
  114.  
  115. -- This code allows you to do any clean-up that might be necessary.
  116. -- Example: you could write the quit event time to a log to see 
  117. -- how often the applet is running.
  118. on quit
  119.     -- do any clean-up chores here
  120.     continue quit
  121. end quit
  122.